/*
 * Copyright (c) 2020-2030 Sishun.Co.Ltd. All Rights Reserved.
 */
package com.rabbitmq.demo.controller;

import com.rabbitmq.demo.bean.Order;
import com.rabbitmq.demo.config.DelayProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * @author ：lvfeilong
 * @version ：
 * @description ：
 * @program ：MySpringBoot
 * @date ：Created in 2021/4/8 13:43
 */
@RestController
@Slf4j
public class RabbitMqDelayController {

    @Resource
    private DelayProducer delayProducer;

    @GetMapping("/send")
    public String send() {
        Order order1 = new Order();
        order1.setBrand("小米11");
        order1.setNum(1);
        order1.setPrice(2999);
        order1.setStatus(0);

        Order order2 = new Order();
        order2.setBrand("IPHONE 12");
        order2.setNum(3);
        order2.setPrice(6999);
        order2.setStatus(1);

        delayProducer.sendOrder(order1);
        delayProducer.sendOrder(order2);
        return "订单准备创建";
    }

    /**
     * PDF转换为图片
     *
     * @param response 响应
     */
    @GetMapping("/test")
    public void test(HttpServletResponse response) {
        File file = new File("D:\\tmp1\\拣货单号RW202101262000003920210305095216.pdf");
        try {
            PDDocument doc = PDDocument.load(file);
            PDFRenderer renderer = new PDFRenderer(doc);
            int pageCount = doc.getNumberOfPages();
            for (int i = 0; i < pageCount; i++) {
                BufferedImage image = renderer.renderImageWithDPI(i, 296);
//          BufferedImage image = renderer.renderImage(i, 2.5f);
                ImageIO.write(image, "PNG", new File("D:\\test.png"));
            }
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        }
    }
}